package com.penghe.www.baselibrary.adapter

import android.content.Context
import android.graphics.Bitmap
import android.support.annotation.ColorInt
import android.support.annotation.DrawableRes
import android.support.annotation.IdRes
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.penghe.www.baselibrary.utils.ImageLoader


/**
 * Created by alm on 17-6-1.
 * 万能适配器专用的ViewHolder
 */

class ViewHolder constructor(context: Context, parent: ViewGroup?, @IdRes layoutId: Int, position: Int) {
    private val mPosition = position
    private val viewList: SparseArray<View> = SparseArray()
    private val convertView: View = LayoutInflater.from(context).inflate(layoutId, parent, false)
    private val imageLoader: ImageLoader = ImageLoader.getInstance(context)

    init {
        convertView.tag = this
    }

    companion object {
        fun get(context: Context, convertView: View?, parent: ViewGroup?, @IdRes layoutId: Int, position: Int): ViewHolder {
            if (convertView == null) {
                return ViewHolder(context, parent, layoutId, position)
            }
            return convertView.tag as ViewHolder
        }
    }

    /**
     * 获取item的整个布局控件
     */
    fun getConvertView(): View {
        return convertView
    }

    /**
     * 根据控件的Id获取控件(先从缓存中获取，如果没有则findViewById,然后保存到缓存)
     *
     * @param viewId 控件的Id
     */
    private fun getView(@IdRes viewId: Int): View {
        var view: View = viewList.get(viewId)
        if (view == null) {
            view = convertView.findViewById(viewId)
            viewList.put(viewId, view)
        }
        return view
    }

    /**
     * 为TextView设置文本内容
     *
     * @param viewId 控件的Id
     * @param text 要设置的文本内容
     */
    fun setText(@IdRes viewId: Int, text: CharSequence): ViewHolder? {
        val view: View = getView(viewId)
        if ((view is TextView)) {
            val textView: TextView = view
            textView.text = text
            return this
        }
        return null
    }

    /**
     * 获取editText的文本
     *
     * @param viewId 控件的Id
     * @return editText的文本
     */
    fun getEditText(@IdRes viewId: Int): CharSequence? {
        val view: View = getView(viewId)
        if (view is EditText) {
            val editText: EditText = view
            return editText.text
        }
        return null
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId 控件的Id
     * @param drawableResId 图片的资源Id
     * @return ViewHolder本类
     */
    fun setImageResource(@IdRes viewId: Int, @DrawableRes drawableResId: Int): ViewHolder? {
        val view: View = getView(viewId)
        if (view is ImageView) {
            val imageView: ImageView = view
            imageView.setImageResource(drawableResId)
            return this
        }
        return null
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId 控件的Id
     * @param bitmap Bitmap图像
     * @return ViewHolder本类
     */
    fun setImageBitmap(@IdRes viewId: Int, bitmap: Bitmap): ViewHolder? {
        val view: View = getView(viewId)
        if (view is ImageView) {
            val imageView: ImageView = view
            imageView.setImageBitmap(bitmap)
            return this
        }
        return null
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId 控件的Id
     * @param url 图片的网络地址
     * @param isCircle 是否将图片显示为圆形
     * @return ViewHolder本类
     */
    fun setImageByUrl(@IdRes viewId: Int, url: String, isCircle: Boolean): ViewHolder?{
        val view: View = getView(viewId)
        if (view is ImageView) {
            val imageView: ImageView = view
            imageLoader.displayImage(url, imageView, isCircle)
            return this
        }
        return null
    }

    /**
     * 给view设置背景色
     *
     * @param viewId 控件的Id
     * @param color 背景色
     * @return ViewHolder本类
     */
    fun setBackgroundColor(@IdRes viewId: Int, @ColorInt color:Int): ViewHolder {
        val view: View = getView(viewId)
        view.setBackgroundColor(color)
        return this
    }

    /**
     * 获得当前的位置
     *
     * @return 当前的位置
     */
    fun getPosition():Int{
        return mPosition
    }
}